package com.autoreassociate;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;

import com.autoreassociate.tool.ConnectionAssociateTool;

public class AlarmSettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	public final static String BUTTON_NUMBER = "buttonNumber";
	
	public final static String PING_SEND_INTERVAL = "ping_send_interval";
	public final static String DEFAULT_PING_SEND_INTERVAL = "60000";
	
	public final static String PING_WAITING_TIME = "ping_waiting_time";
	public final static String DEFAULT_PING_WAITING_TIME = "3000";
	
	public final static String PING_IP_ADDRESS_CUSTOM_SET = "ping_ip_address_custom_set";
	public final static boolean DEFAULT_PING_IP_ADDRESS_CUSTOM_SET = false;
	
	public final static String PING_SEND_IP_ADDRESS = "ping_send_ip_address";
	public static String DEFAULT_PING_SEND_IP_ADDRESS = null;
	
	public final static String IS_ALARM_RTC_WAKEUP = "is_alarm_rtc_wakeup";
	public final static boolean DEFAULT_IS_ALARM_RTC_WAKEUP = false;
	
	public final static String LAST_USE_BUTTON_ID = "last_use_button_id";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.alarm_setting);
		
		//SeekBar disconnectRSSIrate = new SeekBar(getBaseContext());
		//getPreferenceScreen().addPreference(disconnectRSSIrate.get);
		
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		
		
	}
	
	
    @Override
    protected void onPause() {
    	
    	super.onPause();
    	
		int buttonId = getIntent().getIntExtra(BUTTON_NUMBER, 0);
		
		if (buttonId == 0) {
			return;
		}
		
		SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
		Editor editor = getPreferenceScreen().getEditor();
		

		String pingSendInterval = sharedPreferences.getString(PING_SEND_INTERVAL, DEFAULT_PING_SEND_INTERVAL);
		String pingWaitingTime = sharedPreferences.getString(PING_WAITING_TIME, DEFAULT_PING_WAITING_TIME);
		
		try {
			Integer.parseInt(pingSendInterval);
		} catch (Exception e) {
			pingSendInterval = DEFAULT_PING_SEND_INTERVAL;
			ConnectionAssociateTool.getInstance().getMessageLogManager().printForceT("DEFAULT_PING_SEND_INTERVAL : " + DEFAULT_PING_SEND_INTERVAL);
			editor.putString(PING_SEND_INTERVAL, pingSendInterval);
		}
		
		try {
			Integer.parseInt(pingWaitingTime);
		} catch (Exception e) {
			pingWaitingTime = DEFAULT_PING_WAITING_TIME;
			ConnectionAssociateTool.getInstance().getMessageLogManager().printForceT("DEFAULT_PING_WAITING_TIME : " + DEFAULT_PING_WAITING_TIME);
			editor.putString(PING_WAITING_TIME, pingWaitingTime);
		}
		
		editor.putString(PING_SEND_INTERVAL + buttonId, pingSendInterval);
		
		editor.putString(PING_WAITING_TIME + buttonId, pingWaitingTime);
		
		editor.putBoolean(PING_IP_ADDRESS_CUSTOM_SET + buttonId,
				sharedPreferences.getBoolean(PING_IP_ADDRESS_CUSTOM_SET, DEFAULT_PING_IP_ADDRESS_CUSTOM_SET));
		
		editor.putString(PING_SEND_IP_ADDRESS + buttonId,
				sharedPreferences.getString(PING_SEND_IP_ADDRESS, DEFAULT_PING_SEND_IP_ADDRESS));
		
		editor.putBoolean(IS_ALARM_RTC_WAKEUP + buttonId,
				sharedPreferences.getBoolean(IS_ALARM_RTC_WAKEUP, DEFAULT_IS_ALARM_RTC_WAKEUP));
		
		
		editor.commit();
		
		//sendBroadcast(ConnectionCheckReceiver.REQUEST_RESET_INTENT);
		ConnectionAssociateTool.getInstance().attributesReset();
    		
    	if (!isFinishing()) {
    		
    	}
    	
    	
    }
    
    
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    	
        if (key.equals(DEFAULT_PING_SEND_INTERVAL)) {
        }
    	
    	
        if (key.equals(PING_IP_ADDRESS_CUSTOM_SET)) {
        	ConnectionAssociateTool.getInstance().pingIpResetFromDHCP();
        }
    }
    
    
    public static Object[] setStartSettingFromButtonSetting(SharedPreferences sharedPreferences, int buttonId) {
    	
    	int oldButtonId = sharedPreferences.getInt(AlarmSettingActivity.LAST_USE_BUTTON_ID, -1);
    	
    	if (buttonId == oldButtonId) {
        	String pingSendInterval = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_INTERVAL, AlarmSettingActivity.DEFAULT_PING_SEND_INTERVAL);
        	String pingWaitingTime = sharedPreferences.getString(AlarmSettingActivity.PING_WAITING_TIME, AlarmSettingActivity.DEFAULT_PING_WAITING_TIME);
        	Boolean pingIpAddressCustomSet = sharedPreferences.getBoolean(AlarmSettingActivity.PING_IP_ADDRESS_CUSTOM_SET, AlarmSettingActivity.DEFAULT_PING_IP_ADDRESS_CUSTOM_SET);
        	String pingSendIpAddress = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_IP_ADDRESS, AlarmSettingActivity.DEFAULT_PING_SEND_IP_ADDRESS);
        	Boolean isAlarmRtcWakeup = sharedPreferences.getBoolean(AlarmSettingActivity.IS_ALARM_RTC_WAKEUP, AlarmSettingActivity.DEFAULT_IS_ALARM_RTC_WAKEUP);

    		return new Object[] {pingSendInterval, pingWaitingTime, pingIpAddressCustomSet, pingSendIpAddress, isAlarmRtcWakeup};
    	}
    	
    	String pingSendInterval = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_INTERVAL + buttonId, AlarmSettingActivity.DEFAULT_PING_SEND_INTERVAL);
    	String pingWaitingTime = sharedPreferences.getString(AlarmSettingActivity.PING_WAITING_TIME + buttonId, AlarmSettingActivity.DEFAULT_PING_WAITING_TIME);
    	Boolean pingIpAddressCustomSet = sharedPreferences.getBoolean(AlarmSettingActivity.PING_IP_ADDRESS_CUSTOM_SET + buttonId, AlarmSettingActivity.DEFAULT_PING_IP_ADDRESS_CUSTOM_SET);
    	String pingSendIpAddress = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_IP_ADDRESS + buttonId, AlarmSettingActivity.DEFAULT_PING_SEND_IP_ADDRESS);
    	Boolean isAlarmRtcWakeup = sharedPreferences.getBoolean(AlarmSettingActivity.IS_ALARM_RTC_WAKEUP + buttonId, AlarmSettingActivity.DEFAULT_IS_ALARM_RTC_WAKEUP);

		Editor editor = sharedPreferences.edit();
		editor.putString(AlarmSettingActivity.PING_SEND_INTERVAL, pingSendInterval);
		editor.putString(AlarmSettingActivity.PING_WAITING_TIME, pingWaitingTime);
		editor.putBoolean(AlarmSettingActivity.PING_IP_ADDRESS_CUSTOM_SET, pingIpAddressCustomSet);
		editor.putString(AlarmSettingActivity.PING_SEND_IP_ADDRESS, pingSendIpAddress);
		editor.putBoolean(AlarmSettingActivity.IS_ALARM_RTC_WAKEUP, isAlarmRtcWakeup);
		
		
		editor.putInt(AlarmSettingActivity.LAST_USE_BUTTON_ID, buttonId);
		
		editor.commit();
		
		ConnectionAssociateTool.getInstance().attributesReset();
		
		return new Object[] {pingSendInterval, pingWaitingTime, pingIpAddressCustomSet, pingSendIpAddress, isAlarmRtcWakeup};
    }
    
    
    
    public static Object[] getStartSettingFromSelectedSetting(SharedPreferences sharedPreferences) {
    	
    	String pingSendInterval = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_INTERVAL, AlarmSettingActivity.DEFAULT_PING_SEND_INTERVAL);
    	String pingWaitingTime = sharedPreferences.getString(AlarmSettingActivity.PING_WAITING_TIME, AlarmSettingActivity.DEFAULT_PING_WAITING_TIME);
    	Boolean pingIpAddressCustomSet = sharedPreferences.getBoolean(AlarmSettingActivity.PING_IP_ADDRESS_CUSTOM_SET, AlarmSettingActivity.DEFAULT_PING_IP_ADDRESS_CUSTOM_SET);
    	String pingSendIpAddress = sharedPreferences.getString(AlarmSettingActivity.PING_SEND_IP_ADDRESS, AlarmSettingActivity.DEFAULT_PING_SEND_IP_ADDRESS);
    	Boolean isAlarmRtcWakeup = sharedPreferences.getBoolean(AlarmSettingActivity.IS_ALARM_RTC_WAKEUP, AlarmSettingActivity.DEFAULT_IS_ALARM_RTC_WAKEUP);
    	
    	return new Object[] {pingSendInterval, pingWaitingTime, pingIpAddressCustomSet, pingSendIpAddress, isAlarmRtcWakeup};
    }
    
    
    
    
}
